# -*- coding: utf-8 -*-
import numpy as np
# import matplotlib.pyplot as plt
import os
import gauge
import wrf_output


def Decumulation(seq):
    result = [seq[i + 1] - seq[i] for i in range(len(seq) - 1)]
    result.insert(0, seq[0])
    return result


def get_hourly_band_center(path, domain, startTime, endTime):
    # domains = ['01', '02']
    # get every mp_cu group wrf_output and extract the center value
    mp = ['2', '6', '7']
    cu = ['0', '1', '2', '7']
    # split_path = path.split('/')
    # gridSpacing = split_path[-2]
    # scenario = ''
    # if gridSpacing == '139':
    #     scenario = r'Scenario 1'
    # elif gridSpacing == '51545':
    #     scenario = r'Scenario 2'
    # elif gridSpacing == '103090':
    #     scenario = r'Scenario 3'

    results = []
    for i in mp:
        for j in cu:
            mp_cu = i + j
            if os.path.exists(path + mp_cu + '/'):
                input = path + mp_cu + '/wrfout_d' + domain + '_' + startTime
                center = wrf_output.GetCenter(input)
                center = Decumulation(center)
                results.append(center)
    results = np.asarray(results)
    min = np.min(results, axis=0)
    max = np.max(results, axis=0)
    band = [min, max]
    # mean = np.mean(band, axis=0)

    # get the mean gauge value
    gauges_center = gauge.Get50GaugeCenter(startTime, endTime)

    return band, gauges_center


if __name__ == "__main__":
    print("get hourly uncertainty band and center value of gauges")
